<?php        
	include('../database/database.php');
	
	error_reporting(0); session_start();
	ob_start();
   
	if(isset($_GET['addNewCategory'])){
            $categoryName = $_POST['categoryName'];
            $metaTagDescription = $_POST['metaTagDescription'];
            $metaTagKeyWords = $_POST['metaTagKeyWords'];
            $description = $_POST['description'];
            $parentCategory = $_POST['parentCategory'];
            $imageFile = $_POST['imageFile'];
	
            $key = rand();
            
            $query = "insert into category (category_id,category_name,category_meta_description,category_meta_keyword,category_description,parent_category_id,image_url)
             value ('".$key."','".$categoryName."','".$metaTagDescription."','".$metaTagKeyWords."','".$description."','".$parentCategory."','".$imageFile."')";		 
		 
            mysql_query($query);
            $array = array(10);
            $array[0] = $categoryName;
            $array[1] = $metaTagDescription;
            $array[2] = $metaTagKeyWords;
            $array[3] = $description;
            $array[4] = $parentCategory;
            $array[5] = $imageFile;
            
            $_SESSION['categoryCache'][$key] = $array;
	}
        else if(isset($_GET['getAllCategories'])){
            header('Content-type: application/xml');            
            if(!isset($_SESSION['categoryCache'])){                
                $query = "Select * from category order by parent_category_id desc";
                $result = mysql_query($query);
                $array = array(10);
                $arrayMap = array();
                while ($row = mysql_fetch_assoc($result)) {
                    $identifier =  $row['category_id'];
                    $array = array(10);
                    $array[0] = $row['category_name'];
                    $array[1] = $row['category_meta_description'];
                    $array[2] = $row['category_meta_keyword'];
                    $array[3] = $row['category_description'];
                    $array[4] = $row['parent_category_id'];
                    $array[5] = $row['image_url'];                    
                    $arrayMap[$identifier] = $array;                      
                }            
                $_SESSION['categoryCache'] = $arrayMap;                
            }                        
            $response = "<responses>";
            foreach ($_SESSION['categoryCache'] as $key => $value){                
                $pathKey = $key;    
                $fullPath = '';
                while($pathKey!='ROOT'){                                        
                    $fullPath .= ($_SESSION['categoryCache'][$pathKey][0]);                                     
                    $pathKey = $_SESSION['categoryCache'][$pathKey][4];
                    if($pathKey!='ROOT')
                        $fullPath .= " > ";
                }    
                if($fullPath!='')
                    $response .= ("<response id=\"".$key."\" value=\"".$fullPath."\" label=\"".$value[0]."\"></response>");
                else
                    $response .= ("<response id=\"".$key."\" value=\"".$value[0]."\"></response>");
            }
            $response .= "</responses>";            
            echo $response;
        }else if($_GET['getCategoryInformation']){            
            $id = $_GET['categoryId'];            
            $data = $_SESSION['categoryCache'][(string)$id];                        
            $response =  $data[0]."-".$data[1]."-".$data[2]."-".$data[3]."-".$data[4]."-".$data[5];
            echo $response;
        }else if($_GET['deleteCategoryInformation']){
            $id = $_GET['categoryId'];
            $updateQuery = "Update category set parent_category_id='ROOT' where parent_category_id='".$id."'";
            mysql_query($updateQuery);
            $query = "Delete from category where category_id='".$id."'";                        
            mysql_query($query);
            
            unset($_SESSION['categoryCache'][$id]);            
        }
?>